using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace Umbraco.Ecommerce.StoreAdministration
{
	/// <summary>
	/// Summary description for displayorder.
	/// </summary>
	public class DisplayOrder : umbraco.BasePages.UmbracoEnsuredPage
	{
		protected umbraco.uicontrols.Pane Pane1;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.DataGrid Datagrid2;
		protected umbraco.uicontrols.Pane Pane2;
		protected System.Web.UI.WebControls.Literal total;
		protected umbraco.uicontrols.Pane Pane3;
		protected System.Web.UI.WebControls.Literal OrderDate;
		protected umbraco.uicontrols.Pane Pane5;
		protected System.Web.UI.WebControls.Literal paymentMethod;
		protected umbraco.uicontrols.Pane Pane4;
		protected System.Web.UI.WebControls.PlaceHolder plcAddon;
		protected umbraco.uicontrols.UmbracoPanel UmbracoPanel1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			int orderId = int.Parse(Request.QueryString["id"]);
			BusinessLogic.Order o = new BusinessLogic.Order(orderId);
			DataTable dt = new DataTable();
			dt.Columns.Add("key");
			dt.Columns.Add("value");

			OrderDate.Text = 
			paymentMethod.Text = o.PaymentMethod.Name;

			foreach (XmlNode x in o.CustomerInformation.FirstChild.ChildNodes)
			{
				DataRow dr = dt.NewRow();
				dr["key"] = x.ChildNodes[0].InnerText;
				dr["value"] = x.ChildNodes[1].InnerText;
				dt.Rows.Add(dr);
			}
			Datagrid2.DataSource = dt;
			Datagrid2.DataBind();

			dt = new DataTable();
			dt.Columns.Add("ProductId");
			dt.Columns.Add("Name");
			dt.Columns.Add("Quantity");
			dt.Columns.Add("Price");
			dt.Columns.Add("Subtotal");

			foreach (BusinessLogic.OrderLine l in o.OrderLines) {
				DataRow dr = dt.NewRow();
				dr["ProductId"] = l.ProductId;
				dr["Name"] = l.ProductName;
				dr["Quantity"] = l.Quantity;
				dr["Price"] = l.Price.ToString("c",BusinessLogic.Store.NumberFormatInfo);
				dr["Subtotal"] = ((decimal)(l.Quantity * l.Price)).ToString("c",BusinessLogic.Store.NumberFormatInfo);
				dt.Rows.Add(dr);
               }
			DataGrid1.DataSource = dt;
			DataGrid1.DataBind();
			total.Text = Library.FormattedPrice(o.TotalIncVAT);

			try 
			{
				if (BusinessLogic.Store.QuerySettingsFileOutput("//displayordercontrol") != "") 
				{
					try 
					{
						Control uc = new UserControl().LoadControl(BusinessLogic.Store.QuerySettingsFileOutput("//displayordercontrol"));
						plcAddon.Controls.Add(uc);
					} 
					catch 
					{
						plcAddon.Controls.Add(new LiteralControl("Error adding control:" + BusinessLogic.Store.QuerySettingsFileOutput("//displayordercontrol")));
					}
				}
			} 
			catch (Exception ex) {
				plcAddon.Controls.Add(new LiteralControl("Error in component <pre>" + ex.ToString() + "</pre>"));
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}